﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using DAL;

namespace BLL
{
    public class BTrade
    {

        private readonly DTrade _dTrade = new DTrade();
        public  bool Add(Model.TradeInfo model)
        {
            return _dTrade.Add(model);
            
        }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public  int GetMaxId()
        {
            return _dTrade.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public  bool Exists(int id)
        {
            return _dTrade.Exists(id);
        }

          public DataSet GetReport(string strWhere)
          {
              return _dTrade.GetReport(strWhere);
          }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public  bool Update(Model.TradeInfo model)
        {
            return _dTrade.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public  bool Delete(int id)
        {

            return _dTrade.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public  bool DeleteList(string idlist)
        {
            return _dTrade.DeleteList(idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public  Model.TradeInfo GetModel(int id)
        {

            return _dTrade.GetModel(id);
        }

        ///// <summary>
        ///// 得到一个对象实体，从缓存中
        ///// </summary>
        //public Model.TradeInfo GetModelByCache(int id)
        //{

        //    string CacheKey = "ttradeModel-" + id;
        //    object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
        //    if (objModel == null)
        //    {
        //        try
        //        {
        //            objModel = _dTrade.GetModel(id);
        //            if (objModel != null)
        //            {
        //                int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
        //                Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
        //            }
        //        }
        //        catch { }
        //    }
        //    return (Model.TradeInfo)objModel;
        //}

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  DataSet GetList(string strWhere)
        {
            return _dTrade.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  List<Model.TradeInfo> GetModelList(string strWhere)
        {
            DataSet ds = _dTrade.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  List<Model.TradeInfo> DataTableToList(DataTable dt)
        {
            List<Model.TradeInfo> modelList = new List<Model.TradeInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                for (int n = 0; n < rowsCount; n++)
                {
                    Model.TradeInfo model = new Model.TradeInfo();
                    if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
                    {
                        model.Id = int.Parse(dt.Rows[n]["id"].ToString());
                    }
                    if (dt.Rows[n]["Type"] != null && dt.Rows[n]["Type"].ToString() != "")
                    {
                        model.Type = int.Parse(dt.Rows[n]["Type"].ToString());
                    }
                    if (dt.Rows[n]["phoneNumber"] != null && dt.Rows[n]["phoneNumber"].ToString() != "")
                    {
                        model.PhoneNumber = dt.Rows[n]["phoneNumber"].ToString();
                    }
                    if (dt.Rows[n]["TradeTime"] != null && dt.Rows[n]["TradeTime"].ToString() != "")
                    {
                        model.TradeTime = DateTime.Parse(dt.Rows[n]["TradeTime"].ToString());
                    }
                    if (dt.Rows[n]["phoneNumber2"] != null && dt.Rows[n]["phoneNumber2"].ToString() != "")
                    {
                        model.PhoneNumber2 = dt.Rows[n]["phoneNumber2"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public  int GetRecordCount(string strWhere)
        {
            return _dTrade.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return _dTrade.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }


        #endregion  Method
    }
}
